package com.test.ubivelox.thread.pool;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.media.audiofx.Visualizer.OnDataCaptureListener;
import android.os.RemoteException;
import android.provider.MediaStore.Audio.Albums;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class testGalleryAdapter extends BaseAdapter {
	Context mContext;
	ArrayList<AlbumCLS> mList = new ArrayList<AlbumCLS>();
	public testGalleryAdapter(Context context) {
		Log.d("reavelv", "AlbumAdapter");
		mContext = context;
		int x = 0;
		Cursor c = mContext.getContentResolver().query(Albums.EXTERNAL_CONTENT_URI, null, null, null, null);
		if(c.moveToFirst()){
			do{
				try{
				for(String str : c.getColumnNames()){
					Log.d("reavelv", str);
					Log.d("reavelv", c.getString(c.getColumnIndex(str)));
				}}
				catch(NullPointerException e){}
				
				
				try{mList.add(new AlbumCLS(c.getString(c.getColumnIndex("_id")), c.getString(c.getColumnIndex("album")), c.getString(c.getColumnIndex("album_art")), 
						c.getString(c.getColumnIndex("artist"))));}
				catch(NullPointerException e){}
				x++;
				if(x > 10){
					break;
				}
			}while(c.moveToNext());
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		Log.d("reavelv", "getItem - position = " + position);

		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		Log.d("reavelv", "getItemId - position = " + position);
		
		Log.d("reavelv", "getItemId - artist = " + mList.get(position).artist);
		Log.d("reavelv", "getItemId - name = " + mList.get(position).name);
		Log.d("reavelv", "getItemId - id = " + mList.get(position).id);
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d("reavelv", "AlbumAdapter - getView");
		final int pos = position;
		LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(R.layout.item, null);
		
		TextView tv1 = new TextView(mContext);
		tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tv1.setText(mList.get(position).name);
		
		TextView tv2 = new TextView(mContext);
		tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tv2.setText(mList.get(position).artist);
		
		ImageView iv = new ImageView(mContext);
		iv.setLayoutParams(new LayoutParams(100, 100));
		Log.w("reavelv", "" + mList.get(position).icon);
		Bitmap bm = BitmapFactory.decodeFile(mList.get(position).icon);
		iv.setImageBitmap(bm);
		
		LinearLayout ll = (LinearLayout) view.findViewById(R.id.itemll);
		ll.setOrientation(LinearLayout.HORIZONTAL);
		
		ll.addView(iv);
		LinearLayout tll = new LinearLayout(mContext);
		tll.setLayoutParams(new LayoutParams(100, 100));
		tll.setOrientation(LinearLayout.VERTICAL);
		tll.setBackgroundColor(Color.DKGRAY);
		tll.setGravity(Gravity.CENTER);
		ll.addView(tll);
//		view.setOnTouchListener(new OnTouchListener() {
//			
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				
//				return false;
//			}
//		});
		
		tll.addView(tv1);
		tll.addView(tv2);
		return view;
	}

	class AlbumCLS{
		public String id, name, icon, artist;
		AlbumCLS(String id, String name, String icon, String artist){
			this.id = id; this.name = name; this.icon = icon; this.artist = artist;
		}
	}
}
